#include "helper.h"
#include "monitor.h"
#include "reg.h"

extern uint32_t instr;
extern char assembly[80];

static void decode_j_type(uint32_t instr) {

    op_dest->type = OP_TYPE_JUMP;
    op_dest->imm = instr & INDEX_MASK;
}

make_helper(j) {

    decode_j_type(instr);
    cpu.next_pc = ((cpu.pc & 0xF0000000) | (op_dest->imm << 2)) - 4;
    sprintf(assembly, "j   0x%08x", op_dest->imm);
    temu_state = JUMP;
}

make_helper(jal) {

    decode_j_type(instr);
    reg_w(R_RA) = cpu.pc + 8;
    cpu.next_pc = ((cpu.pc & 0xF0000000) | (op_dest->imm << 2)) - 4;
    sprintf(assembly, "jal   0x%08x", op_dest->imm);
    temu_state = JUMP;
}